<?php
/** 
 * 公共模型
 */
namespace Base\Model;
use Think\Model;

class BaseModel extends Model {

	/**
	 * dataList
	 */
	final public function dataList($extPageBean) {
		// $where
		$where = array();
		
		// equal
		$equalMap = $extPageBean['equalMap'];
		if ($equalMap) {
    		foreach ($equalMap as $k => $v) {
    			if ($v != NULL) {
    				$where[$k] = ['eq', $v];
    			}
    		}
		}
		
		// NOT equal
		$notEqualMap = $extPageBean['notEqualMap'];
		if ($notEqualMap) {
    		foreach ($notEqualMap as $k => $v) {
    			if ($v != NULL) {
    				$where[$k] = ['neq', $v];
    			}
    		}
		}
		
		// like
		$likeMap = $extPageBean['likeMap'];
		if ($likeMap) {
		    foreach ($likeMap as $k => $v) {
		        if ($v != NULL) {
		            $where[$k] = ['like', "%$v%"];
		        }
		    }
		}

		// 拼接in
		$inKey = $extPageBean['inKey'];
		$inValue = $extPageBean['inValue'];
		if ($inKey) {
		    $where[$inKey] = ['in', $inValue];
		}
		
		// offset and limit
		$offset = $extPageBean['offset'];
		$limit = $extPageBean['limit'];
		
		// sort and order
		$sort = $extPageBean['sort'];
		$order = $extPageBean['order'];
		if ($sort) {
			$dataList['rows'] = $this->where($where)->order($sort . ' ' . $order)->limit($offset, $limit)->select();
		} else {
	    	$dataList['rows'] = $this->where($where)->limit($offset, $limit)->select();
		}
// 		dump($where);
		
		// data list
	    $dataList['total']  = $this->where($where)->count();
// 	    $dataList['rows'] = $this->where($where)->order($sort . ' ' . $order)->limit($offset, $limit)->select();
	    
	    // return
	    return $dataList;
	}
	
	/**
	 * sortOrder
	 */
	protected function sortOrder($sortOrders, $customField = '') {
		$field = empty ($customField) && !is_string($customField) ? 'sort_order' : $customField;
		$pk = $this->getPk();
		foreach ($sortOrders as $k => $v) {
			$data[$field] = $v;
			$this->where(array($pk => $k))->save($data);
		}
		return true;
	}

    /**
     * 删除表
     */
    final public function drop_table($tablename) {
        $tablename = C("DB_PREFIX") . $tablename;
        return $this->query("DROP TABLE $tablename");
    }

    /**
     * 读取全部表名
     */
    final public function list_tables() {
        $tables = array();
        $data = $this->query("SHOW TABLES");
        foreach ($data as $k => $v) {
            $tables[] = $v['tables_in_' . strtolower(C("DB_NAME"))];
        }
        return $tables;
    }

    /**
     * 检查表是否存在 
     * $table 不带表前缀
     */
    final public function table_exists($table) {
        $tables = $this->list_tables();
        return in_array(C("DB_PREFIX") . $table, $tables) ? true : false;
    }

    /**
     * 获取表字段 
     * $table 不带表前缀
     */
    final public function get_fields($table) {
        $fields = array();
        $table = C("DB_PREFIX") . $table;
        $data = $this->query("SHOW COLUMNS FROM $table");
        foreach ($data as $v) {
            $fields[$v['Field']] = $v['Type'];
        }
        return $fields;
    }

    /**
     * 检查字段是否存在
     * $table 不带表前缀
     */
    final public function field_exists($table, $field) {
        $fields = $this->get_fields($table);
        return array_key_exists($field, $fields);
    }
    
    protected function _before_write(&$data) {
        
    }

}
